package luren.king.wechatlite.activity;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import java.util.ArrayList;
import java.util.List;

import luren.king.wechatlite.R;
import luren.king.wechatlite.adapter.ChatAdapter;
import luren.king.wechatlite.config.Config;
import luren.king.wechatlite.database.MessageDataBaseHelper;
import luren.king.wechatlite.pojo.ChatMessage;

public class MessageActivity extends AppCompatActivity {
    private RecyclerView recyclerView;
    private ChatAdapter chatAdapter;
    private List<ChatMessage> chatMessageList;


    @SuppressLint("NotifyDataSetChanged")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EdgeToEdge.enable(this);
        setContentView(R.layout.activity_message);

//        这里获取到了传过来的唯一参数，通过唯一参数搜索数据库
        String value = getIntent().getStringExtra("Email");
        String Name = getIntent().getStringExtra("Name");
//        Toast.makeText(this, value, Toast.LENGTH_LONG).show();

        EditText message = findViewById(R.id.send_message_content);
        Button Send_btn = findViewById(R.id.send_button);
        TextView title = findViewById(R.id.tv_chat_title);
        title.setText(Name);

        recyclerView = findViewById(R.id.message_contain);
        chatMessageList = new ArrayList<>();

        // 初始化适配器和 RecyclerView
        chatAdapter = new ChatAdapter(chatMessageList);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        recyclerView.setAdapter(chatAdapter);

        // todo 从数据库里面读取聊天记录并渲染到 RecyclerView
        MessageDataBaseHelper messageDataBaseHelper = new MessageDataBaseHelper(this);
        List<ChatMessage> allMessages = messageDataBaseHelper.getAllMessages(value);
        for (ChatMessage Message : allMessages) {
            addMessage(new ChatMessage(Message.getContent(), Message.getType()));
        }

        Config.WEB_SOCKET.setRecyclerView(recyclerView, chatAdapter, chatMessageList, this);

        Send_btn.setOnClickListener(v -> {
            String messageText = message.getText().toString();
            if (messageText.isEmpty()) {
                return;
            }
            Config.WEB_SOCKET.sendMessage(messageText, value);
            message.setText("");
        });

        ImageView back = findViewById(R.id.MessageBack);
        back.setOnClickListener(v -> {
            finish();
        });

        ImageView options = findViewById(R.id.btn_options);
        options.setOnClickListener(v -> {
            Toast.makeText(this, "功能暂未开放", Toast.LENGTH_SHORT).show();
        });
    }

    private void addMessage(ChatMessage message) {
        chatMessageList.add(message);
        chatAdapter.notifyItemInserted(chatMessageList.size() - 1);
        recyclerView.scrollToPosition(chatMessageList.size() - 1);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        Config.WEB_SOCKET.deleteRecyclerView();
        Config.WEB_SOCKET.setSender("");
    }
}